/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.BanTinDao;
import dao.KiemTra;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.BanTinPoJo;

/**
 *
 * @author Kanet
 */
@WebServlet(name = "bantin", urlPatterns = {"/HienBanTin"})
public class HienBanTin extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        /*response.setContentType("text/html;charset=UTF-8");*/
        request.setCharacterEncoding("UTF-8");
       /* PrintWriter out = response.getWriter();*/
        HttpSession session=request.getSession();
//        
        try {
            
            String ac=request.getParameter("ac");       
            /**
             * a: thêm bản tin
             * v: hiện bản tin
             */
            if(ac.equals("a")==true){
               
            }
            if(ac.equals("v")==true){
                this.HienBanTin(request,response,session);
            }
            
        } catch (Exception ex) {
		ex.printStackTrace();
	}
    }
//    
//    private void TaoBanTin(){
//        
//    }
    private void HienBanTin(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws ParseException, ServletException, IOException{
        String dichVu=request.getParameter("dv");
        String danhMuc=request.getParameter("dm");
        String trang=request.getParameter("p");
        ArrayList<BanTinPoJo> dsBanTin=null;
        if(dichVu.isEmpty()==true&& danhMuc.isEmpty()==true&&trang.isEmpty()==true){
                if(KiemTra.isNumber(dichVu)==true && KiemTra.isNumber(danhMuc)==true && KiemTra.isNumber(trang)==true){
                    int idDichVu=Integer.parseInt(dichVu);
                    int idDanhMuc=Integer.parseInt(danhMuc);
                    if(danhMuc.equals("0")==true){
                        dsBanTin=BanTinDao.LayDanhSachTheoDichVu(idDichVu);
                        session.setAttribute("dsBanTin", dsBanTin);
                        request.getRequestDispatcher("/index.jsp?f=4").forward(request, response);
                        return;
                    }else{
                        dsBanTin=BanTinDao.LayDanhSachTheoDichVu(idDanhMuc);
                        session.setAttribute("dsBanTin", dsBanTin);
                        request.getRequestDispatcher("/chovoucher.jsp").forward(request, response);
                        return;
                    }
                }
            }else{
                return;
            }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(HienBanTin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(HienBanTin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    
}
